package com.jt.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by FanJia on 2020/11/12 10:59
 */
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {


    @Value("${redis.nodes}")
    private String nodes;

    @Bean
    public JedisCluster jedisCluster(){
        Set<HostAndPort> sets = new HashSet<>();

        String[] nodeArray = nodes.split(",");
        for(String node : nodeArray){
            String host = node.split(":")[0];
            int port = Integer.parseInt(node.split(":")[1]);
            sets.add(new HostAndPort(host, port));
        }

        return new JedisCluster(sets);
    }




 /*   @Value("${redis.nodes}")
    private String nodes;
    @Bean
    public ShardedJedis shardedJedis(){

        List<JedisShardInfo> shardedJedis = new ArrayList<>();

        String[] nodeArray = nodes.split(",");
        for(String node : nodeArray){
            String host = node.split(":")[0];
            int port = Integer.parseInt(node.split(":")[1]);
            shardedJedis.add(new JedisShardInfo(host, port));
        }

        return new ShardedJedis(shardedJedis);
    }
*/


    /*@Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private Integer port;


    @Bean
    public Jedis jedis(){
        return new Jedis(host,port);
    }*/
}
